package com.vmladenov.cook.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.vmladenov.cook.CustomApplication;
import com.vmladenov.cook.R;
import com.vmladenov.cook.core.DialogHelper;
import com.vmladenov.cook.core.Helpers;
import com.vmladenov.cook.core.html.Recipe;
import com.vmladenov.cook.core.objects.MainActivityState;
import com.vmladenov.cook.core.objects.User;
import com.vmladenov.cook.ui.recipes.AllRecipesActivity;
import com.vmladenov.cook.ui.recipes.FavoriteRecipesActivity;
import com.vmladenov.cook.ui.recipes.LatestRecipesActivity;
import com.vmladenov.cook.ui.recipes.ViewRecipeActivity;
import com.vmladenov.cook.ui.useful.AdvicesActivity;
import com.vmladenov.cook.ui.useful.ProductsActivity;
import com.vmladenov.cook.ui.useful.SpicesActivity;

public final class MainActivity extends Activity
{
	private Boolean loadingRecipeOfTheDay;
	Recipe recipeOfTheDay = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		loadUserId();
		MainActivityState state = (MainActivityState) getLastNonConfigurationInstance();
		if (state == null || state.recipe == null)
			getRecipeOfTheDay();
		else
			showRecipeOfTheDay(state.recipe);
	}

	private void getRecipeOfTheDay()
	{
		loadingRecipeOfTheDay = true;
		Thread initializeThread = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				recipeOfTheDay = Helpers.getDataHelper().RecipesRepository.getRecipeOfTheDay();
				runOnUiThread(new Runnable()
				{
					@Override
					public void run()
					{
						showRecipeOfTheDay(recipeOfTheDay);
					}
				});
			}
		});
		initializeThread.start();
	}

	private void loadUserId()
	{
		SharedPreferences preferences = getSharedPreferences("MandjaSettings", MODE_PRIVATE);
		User user = new User(preferences.getLong("UserId", -1), preferences.getString("UserName", ""));
		CustomApplication.getInstance().setCurrentUser(user);
		if (user.getName() != "")
		{
			String title = getString(R.string.app_name) + " - " + getString(R.string.user) + " : " + user.getName();
			this.setTitle(title);
		}
	}

	private void showRecipeOfTheDay(Recipe recipe)
	{
		if (recipe == null) return;

		TextView txtRecipeContent = (TextView) findViewById(R.id.txtRecipeContent);
		ImageView imgRecipePicture = (ImageView) findViewById(R.id.imgRecipePicture);

		txtRecipeContent.setText(recipe.getTitle());
		Helpers.setImageFromUrlAsync(imgRecipePicture, recipe.getImageUrl());
		loadingRecipeOfTheDay = false;
	}

	public void onLatestRecipeClick(View v)
	{
		if (loadingRecipeOfTheDay || recipeOfTheDay == null) return;
		Bundle bundle = new Bundle();
		bundle.putLong("id", recipeOfTheDay.getId());

		Intent intent = new Intent();
		intent.setClass(MainActivity.this, ViewRecipeActivity.class);
		intent.putExtra("RECIPE_ID", bundle);

		startActivity(intent);
	}

	public void onLatestRecipesClick(View v)
	{
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, LatestRecipesActivity.class);
		startActivity(intent);
	}

	public void onAllRecipesClick(View v)
	{
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, AllRecipesActivity.class);
		startActivity(intent);
	}

	// public void onCategoryRecipesClick(View v) {
	// Intent intent = new Intent();
	// intent.setClass(MainActivity.this, RecipeCategoriesActivity.class);
	// startActivity(intent);
	// }

	public void onAdvicesClick(View v)
	{
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, AdvicesActivity.class);
		startActivity(intent);
	}

	public void onSpicesClick(View v)
	{
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, SpicesActivity.class);
		startActivity(intent);
	}

	public void onProductsClick(View v)
	{
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, ProductsActivity.class);
		startActivity(intent);
	}

	public void onShowFavoriteRecipes(View v)
	{
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, FavoriteRecipesActivity.class);
		startActivity(intent);
	}

	public void onToolsClick(View v)
	{
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, ToolsActivity.class);
		startActivity(intent);
	}

	@Override
	public Object onRetainNonConfigurationInstance()
	{
		MainActivityState state = new MainActivityState();
		state.recipe = recipeOfTheDay;
		return state;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.miAbout:
				DialogHelper.showAbout(MainActivity.this);
				return true;
			case R.id.miSettings:
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, SettingsActivity.class);
				startActivity(intent);
				return true;
			case R.id.miLogIn:
				DialogHelper.showLogInDialog(MainActivity.this);
				return true;
			default:
				return super.onMenuItemSelected(featureId, item);
		}
	}

}
